<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Gass_message_model extends CI_Model {
	
	// The following method prevents an error occurring when $this->data is modified.
	// Error Message: 'Indirect modification of overloaded property Demo_cart_admin_model::$data has no effect'.
	public function &__get($key)
	{
		$CI =& get_instance();
		return $CI->$key;
	}
	/**
	 * new_message
	 * 编辑提交新的点对点的站内信
	 */
	 function new_message($sendId,$recId,$content){
	 	
		 $sm_data = array(
       'send_id' => $sendId,
       'rec_id' => $recId,
       'mess_status' => 1
     );	     
     $this->db->trans_start();
     $this->db->insert('ga_station_mess', $sm_data); 	
     $mc_data = array(
       'mess_id' => $this->db->insert_id(),
       'mess_content' => $content,
       'creat_time' => date('Y-m-d H:i:s',time())
     );	

     $this->db->insert('ga_mess_content', $mc_data);
     $this->db->trans_complete();
     
	 }
	 
	/**
	 * 获取个人信息列表
	 * 
	 * 注意排除已经被置为过期（即已删除）的信息
	 */
	 function get_message_list($uId)
	 {
	 	 $search_query="";
	 	 $uri = $this->uri->uri_to_assoc(4);	 
	 	 $this->db->distinct();
	 	 $this->db->select('send_id,rec_id');
	 	 $this->db->from('message_list_view');
	 	 $where = "((send_id = '".$this->flexi_auth->get_user_id()."' and send_expired = 1) 
	 	 or (rec_id = '".$this->flexi_auth->get_user_id()."' and rec_expired = 1))";
	 	 if (!empty($uri['search']))
	 	 {
	 	 	 $where = $where." and (mess_content like '%".urldecode($uri['search'])."%' or upro_first_name like '%".urldecode($uri['search'])."%' or upro_last_name like '%".urldecode($uri['search'])."%')";
	 	 	 $search_query = urldecode($uri['search']);
	 	 }
	 	 $this->db->where($where);
	 	 $this->data['messageList'] = $this->db->get()->result_array();
	 	 
		 $this->data['search_text'] = $search_query;
	 }	 
	 
 	/**
 	 * message_session
 	 * 根据会话双方的ID获取双方的交谈记录，并判断、获取接收方信息
 	 */
	 function message_session($fId,$sId){
	 	
	 	 $this->db->select('*');
	 	 $this->db->from('message_list_view');
	 	 
	 	 switch($this->flexi_auth->get_user_id()){
	 	 	 case $fId:
	 	   $where = "(send_id = '".$fId."' and rec_id = '".$sId."' and send_expired = 1) or 
	 	           (rec_id = '".$fId."' and send_id = '".$sId."' and rec_expired = 1)";	 	
	 	   break; 	 
	 	 	 case $sId:
	 	   $where = "(send_id = '".$fId."' and rec_id = '".$sId."' and rec_expired = 1) or 
	 	           (rec_id = '".$fId."' and send_id = '".$sId."' and send_expired = 1)";	 	
	 	   break;
	 	 }

	 	 $this->db->where($where);
	 	 $this->db->order_by('creat_time');
	 	 $this->data['messageList'] = $this->db->get()->result_array();
	 	 //判断接收方
	 	 if(!empty($this->data['messageList'])){
	 	 	 $recId = ($this->data['messageList'][0]['send_id']==$this->flexi_auth->get_user_id())?$this->data['messageList'][0]['rec_id']:$this->data['messageList'][0]['send_id'];
	 	   $this->data['recUserInfo'] = $this->flexi_auth->get_user_by_id($recId)->row_array();
	 	 }
	 }	
	  
 	/**
 	 * session_del
 	 * 根据会话双方的ID设置相应的信息未过期
 	 */	 
	 function session_del($fId,$sId){ 	
	 	 
	 	 switch($this->flexi_auth->get_user_id()){
	 	 	 case $fId:
         $sendData = array('send_expired' => 2);	 	 
         $this->db->where("( send_id = '".$fId."' and rec_id = '".$sId."' and send_expired = 1)");
         $this->db->update('ga_station_mess', $sendData);
         	 	 	 
         $recData = array('rec_expired' => 2);	 	 
         $this->db->where("( send_id = '".$sId."' and rec_id = '".$fId."' and rec_expired = 1)");
         $this->db->update('ga_station_mess', $recData);
         break; 		 	 	 
         
	 	 	 case $sId:
	 	 	 
         $recData = array('rec_expired' => 2);	 	 
         $this->db->where("( send_id = '".$fId."' and rec_id = '".$sId."' and rec_expired = 1)");
         $this->db->update('ga_station_mess', $recData);
         	 	 	 
         $sendData = array('send_expired' => 2);	 	 
         $this->db->where("( send_id = '".$sId."' and rec_id = '".$fId."' and send_expired = 1)");
         $this->db->update('ga_station_mess', $sendData);
         break;
	 	 }
	 	
	 
     
     redirect('gass_message/message_list/'.$this->flexi_auth->get_user_id()); 	 

	 }
}